package com.sample.tdf.generator;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;

/**
 * 通过调用 Domain JoinDomain TreeDomain 生成TDFbase所有结构代码
 */
public class TdfBaseGenerator {
    public static void main(String[] args) {
        //是否是maven多模块工程
        boolean isMoudle = true;
        //模块路径 如果不是多模块可以不填
        String moudlePath = "tdf-sys-sample-mybatis";

        //数据库相关
        String jdbcUrl = "jdbc:mysql://localhost:3306/tdf?serverTimezone=GMT%2B8&amp&useSSL=false&characterEncoding=utf-8";
        String jdbcDriver = "com.mysql.cj.jdbc.Driver";
        String userName = "root";
        String password = "root";

        //包配置
        //父包名
        String parentPackageName = "com.sample.tdf";
        //代码模块名  会在父包名的目录下创建名字为moudelName的文件夹
        String moudelName = "student";
        //controller代码的目录名称
        String controller = "api";
        //实体目录名称
        String entity = "domain";

        //domain结构表
        String[] domainTables = {"student"};
        //中间表
        String[] joinTables = {"course_student_score"};
        //tree结构表
        String[]  treeTables = {"school_grade"};
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        String path = projectPath + "\\" + getMoudlePath(isMoudle, moudlePath) + "\\src\\main";
        String outPutDir = path+"\\java";
        gc.setOutputDir(outPutDir);
        gc.setAuthor("陈哲");
        gc.setOpen(false);
        gc.setIdType(IdType.ASSIGN_UUID); //自动生成uuid
        gc.setBaseColumnList(true);       //添加BaseColumnList在xml
        gc.setBaseResultMap(true);        //添加BaseResultMap在xml
        gc.setSwagger2(true);             //使用实体属性 Swagger2 注解

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(jdbcUrl);
//         dsc.setSchemaName("public");
        dsc.setDriverName(jdbcDriver);
        dsc.setUsername(userName);
        dsc.setPassword(password);
        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(moudelName);
        pc.setParent(parentPackageName);
        pc.setController(controller);
        pc.setEntity(entity);

        DomainGenerator.generator(gc,dsc,pc,domainTables,path);
        JoinDomainGenerator.generator(gc,dsc,pc,joinTables,path);
        TreeDomainGenerator.generator(gc,dsc,pc,treeTables,path);
    }

    public static String getMoudlePath(boolean isMoudle, String moudelPath) {
        if (isMoudle) {
            return  moudelPath;
        } else {
            return "";
        }
    }

    ;

    public static String getParent(String parent) {
        String[] split = StrUtil.split(parent, ".");
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            stringBuilder.append(s).append("/");
        }
        return stringBuilder.toString();
    }
}
